package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"litter-api/app/model"
	service2 "litter-api/app/service"
	"litter-api/app/util"
)

type UserHandler struct {
	service *service2.UserService
	base    *Base
}

func NewUserHandler() *UserHandler {
	return &UserHandler{}
}

// Index
/**
 *author:Leedong
 *notes:用户信息
 *date:2:41 下午 2022/7/30
 **/
func (h *UserHandler) Index(ctx *gin.Context) {
	res, err := h.service.Info()
	if err != nil {
		util.Failed(ctx, "query fails!")
		return
	}
	util.Success(ctx, res)
}

// Wallet
/**
 *author:Leedong
 *notes:
 *date:2:40 下午 2022/7/30
 **/
func (h *UserHandler) Wallet(ctx *gin.Context) {
	res, err := h.service.Wallet()
	if err != nil {
		util.Failed(ctx, "query fails!")
		return
	}
	util.Success(ctx, res)
}

func (h *UserHandler) AddrInfo(ctx *gin.Context) {
	res, err := h.service.UserAddr()
	if err != nil {
		util.Failed(ctx, "query fails!")
		return
	}
	util.Success(ctx, res)
}

func (h *UserHandler) SetAddr(ctx *gin.Context) {
	var Addr model.UserAddr
	if err := ctx.ShouldBind(&Addr); err != nil {
		fmt.Println(err)
		util.Failed(ctx, err)
		return
	}
	if err := h.service.SetAddr(Addr); err != nil {
		util.Failed(ctx, "Setup failed!")
		return
	}
	util.Success(ctx, Addr)
}
